﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK.Service
{
    public class CustomerService : BaseService, ICustomerService
    {
        private readonly ICustomerRepository _customerRepository;
        private readonly IUnitOfWork _unitOfWork;
        public CustomerService(IMapper mapper, ICustomerRepository _customerRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._customerRepository = _customerRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddCustomer(Customer customer)
        {
            _customerRepository.AddEntity(customer);
            return _unitOfWork.SaveChangesEntity();
        }
        public bool AddCustomerList(List<Customer> customer,out string message)
        {
            message = string.Empty;
            foreach (Customer item in customer)
            {

                item.Id = YitIdHelper.NextId();

                var aobj = _customerRepository.QueryIsEntityWhere(x => x.Code == item.Code);
                if (aobj != null)
                {
                    message = $"{item.Code}编码已存在！";
                    return false;
                }

            }
            _customerRepository.AddEntityList(customer);
            return _unitOfWork.SaveChangesEntity();
        }
        public bool DelCustomer(long id)
        {
            var cust = _customerRepository.QueryEntityById(id);
            if (cust !=null)
            {
               _customerRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<Customer> GetAllCustomer(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _customerRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _customerRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }
       public List<Customer> GetCascadeCustomerData()
        {
            // 1. 获取所有仓库数据（假设从数据库或服务层获取）
            List<Customer> Customers = _customerRepository.GetAllEntityList();
            // 3. 构建树形结构
            var parentNodes = Customers
                .Where(s => s.Code.Count(c => c == '.') == 0) // 第一级编码格式：XX.XX
                .OrderBy(s => s.Code)
                .ToList();

            var childNodes = Customers
                .Where(s => s.Code.Count(c => c == '.') == 1) // 第二级编码格式：XX.XX.XX
                .ToList();

            // 4. 建立父子关系
            foreach (var parent in parentNodes)
            {
                string parentCodePrefix = $"{parent.Code}.";
                parent.Children = childNodes
                    .Where(c => c.Code.StartsWith(parentCodePrefix))
                    .OrderBy(c => c.Code)
                    .ToList();
            }

            return parentNodes;
        }

        public Customer? GetCustomerById(long id)
        {
            return _customerRepository.QueryEntityById(id);
        }

        public bool UpdCustomer(Customer Customer)
        {
            var aobj = _customerRepository.QueryIsEntityWhere(x => x.Id != Customer.Id && x.Code == Customer.Code);
            if (aobj != null)
            {
                return false;
            }
            _customerRepository.UpdEntity(Customer);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
